package com.futurelight.recipes.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.futurelight.recipes.R;

import java.util.List;

public class CandidateAdapter extends BaseAdapter {

    public void setCandidate(List<String> candidate) {
        this.candidate = candidate;
        Log.v("ddd","hello"+candidate.toString());
        notifyDataSetChanged();
    }

    public List<String> candidate;
    private View itemView;
    private Holder holder;
    private CallBackResult listener;

    @Override
    public int getCount() {
        if (candidate == null){
            return 0;
        }
        return candidate.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setCallBack(CallBackResult listener){
        this.listener = listener;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (itemView == null) {
            holder = new Holder();
            itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_candidate, parent, false);
            holder.textView = itemView.findViewById(R.id.candidate_text);
            holder.textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.callText(((TextView)v).getText().toString());
                }
            });
            itemView.setTag(holder);

        }else{
            holder = (Holder) itemView.getTag();
        }
        holder.textView.setText(candidate.get(position));

        return itemView;
    }
    class Holder {
        TextView textView;
    }
}